$(".js_game_btn").click(function(){
	var letters = $('.js_game_tab>li.active').text();
	//Ajax
	search_games(letters);

	$(".js_server").css("display","none");
	if($(".js_game").css("display")=="none"){
		$(".js_game").css("display","block");
	}else{
		$(".js_game").css("display","none");
	}
});

$('.search').click(function(){
	if($(this).hasClass('active')) return false;
	var letters = $(this).text();
	//Ajax
	search_games(letters);
})

$(".js_server_btn").click(function(){
	if($(".js_game_btn").text()=="选择充入的游戏"){
		$(".js_server").css("display","none");
		
	}else{
			
		if($(".js_server").css("display")=="none"){
			$(".js_server").css("display","block");
		}else{
			$(".js_game").css("display","none");
			$(".js_server").css("display","none");
		}
	}
});

$(".js_list_close").click(function(){
	$(this).parent().css("display","none");	
});
$(".js_game_tab li").click(function(){
	$(".js_game_tab li").removeClass("active");
	$(this).addClass("active");
});
$(".js_server_tab li").click(function(){
	$(".js_server_tab li").removeClass("active");
	$(this).addClass("active");
});

//订单金额
function order_amount(){
	var order_amount = $('.input_chk:checked').val();
	if( order_amount === 'other' ){
		order_amount = $('.js_input_txt').val();
	}
	return order_amount;
}

$('.btn_pay').click(function(){

	var gid       = $('#gid').val();
	var sid       = $('#sid').val();
	var pay       = $('input[name="pay_method"]').val();
	var charge_to = $('.charge-to:checked').val();
	var IS_ALIPAY = false;

	if( pay === 'alipay' ){
		IS_ALIPAY = true;
	}

	if( charge_to == 'game' && (! gid || ! sid) ){
		alert('请选择您要充值的游戏和游戏服务器');
		return false;
	}

	var order_fee = order_amount();

	if( ! order_fee ){
		alert('请选择充值金额');
		return false;
	}


	if( confirm('您确定要充值吗?') ){

		if( ! IS_ALIPAY ){
			layer.open({
				type: 2,
				area: ['450px', '235px'],
				title: '支付验证',
				fix: false,
				content: '/Pay/checkCode'
			});
		}else{
			$('#pay-form').submit();
		}

	}
})

$('.charge-to').change(function(){
	var v = $(this).val();
	if(v === 'tb'){
		$('#game-selector').hide();
		$('#tb-alert').show();
	}else{
		$('#game-selector').show();
		$('#tb-alert').hide();
	}
});

$('input[name="order_amount"]').change(function(){
	show_currency();
});

$('.input_txt').focus(function(){
	$('input.js_other').attr('checked', 'checked');
});


$(".input_txt").keyup(function(){
	var _this = $(this);
	var v = _this.val();
	_this.val( v.replace(/\D+/, '') );
});

$('.input_txt').blur(function(){
	show_currency();
});

function search_games(letters){
	$.post('/Api/GameList/', {letters: letters}, function(data){
		var d = data;
		var g = data;
		var h = '';
		for( var i = 0; i< d.length; i++ ){
			h += '<li><a href="#" class="game-title" gid="'+ d[i]['game_id'] +'">'+ d[i]['game_name'] + '</a></li>';
		}
		$('.js_gameli').html(h);

		$('.game-title').click(function(){
			var gid = $(this).attr('gid');
			var p = $(this).parent().index();

			$('input#gid').val(gid);
			$('.js_serverli').html('');
			$('.js_server_btn').text($('.js_server_btn').attr('title'));
			$.post('/Api/GameServer/', {gid: gid}, function(data){
				d = data;
				h = '';
				var currency = d['currency'];
				note_rate(currency);
				show_currency();

				for( var i in d ){
					if( i === 'currency' ) continue;
					h += '<li><a href="#" class="server-title" sid="'+ d[i]['game_server_id'] +'">'+ d[i]['game_server_name'] + '</a></li>';
				}
				$('.js_serverli').html(h);

				$(".server-title").click(function(){
					var sid = $(this).attr('sid');
					$('input#sid').val(sid);
					$(".js_server_btn").text($(this).text());
					$(".js_server").css("display","none");

					var pt = $('#pay-type');
					if(pt.length > 0){
						var gpt = g[p]['game_recharge_class'];
						var o = $('input.ye-charge');
						if( gpt == 12 ){
							o.attr('disabled', 'disabled');
							o.next('abbr').css('color', '#ccc');
						}else{
							o.removeAttr('disabled');
							o.next('abbr').css('color', 'black');
						}
					}

				});

			})
			$(".js_game_btn").text($(this).text());
			$(".js_game").css("display","none");
			$(".js_server").css("display","block");
		});
	})
}

function note_rate(currency){
	if(currency['rate'] > 0){
		$('#rate').val(currency['rate']);
		$('#currency_name').html(currency['currency']);
	}else{
		$('#rate').val('0');
		$('#currency_name').html('');
	}
}

function show_currency(){
	var order_fee = order_amount();
	var rate      = $('#rate').val();
	if( order_fee > 0 && rate > 0 ){
		$('#currency_amount').html(order_fee * rate);
		$('#currency_ratio').html('1:' + rate);
		$('#game_currency').show();
	}else{
		$('#game_currency').hide();
	}
}